package com.isoftstone.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoftstone.wms.common.QueryPageParam;
import com.isoftstone.wms.common.Result;
import com.isoftstone.wms.entity.Goods;
import com.isoftstone.wms.service.impl.GoodsService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2025-10-03
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        try {
            System.out.println("Saving goods: " + goods);
            boolean result = goodsService.save(goods);
            System.out.println("Save result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("保存失败: " + e.getMessage());
        }
    }

    @PostMapping("/mod")
    public Result mod(@RequestBody Goods goods) {
        try {
            System.out.println("Updating goods: " + goods);
            boolean result = goodsService.updateById(goods);
            System.out.println("Update result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败: " + e.getMessage());
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody HashMap<String, Object> params) {
        try {
            Integer id = (Integer) params.get("id");
            System.out.println("Deleting goods with id: " + id);
            boolean result = goodsService.removeById(id);
            System.out.println("Delete result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/listpage")
    public Result listpageC(@RequestBody QueryPageParam query) {
        try {
            // 获取前端传过来的查询参数
            HashMap<String, Object> param = query.getParam();
            String name = null;
            Integer storageId = null; // 仓库ID
            Integer goodstypeId = null; // 分类ID
            
            // 按货名模糊查询
            if(param.containsKey("name")) {
                Object nameObj = param.get("name");
                if(nameObj != null && nameObj instanceof String) {
                    name = (String) nameObj;
                }
            }
            
            // 按仓库ID精确查询
            if(param.containsKey("storage")) {
                Object storageObj = param.get("storage");
                if(storageObj != null && storageObj instanceof Integer) {
                    storageId = (Integer) storageObj;
                }
            }
            
            // 按分类ID精确查询
            if(param.containsKey("goodstype")) {
                Object goodstypeObj = param.get("goodstype");
                if(goodstypeObj != null && goodstypeObj instanceof Integer) {
                    goodstypeId = (Integer) goodstypeObj;
                }
            }

            // 1. 创建分页对象
            Page<Goods> page = new Page<>();
            page.setCurrent(query.getPageNum()); // 当前页码
            page.setSize(query.getPageSize());  // 每页条数
            
            // 2. 创建条件构造器
            LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
            
            // 按货名模糊查询
            if(StringUtils.isNotBlank(name)){
                wrapper.like(Goods::getName, name);
            }
            
            // 按仓库ID精确查询
            if(storageId != null){
                wrapper.eq(Goods::getStorage, storageId);
            }
            
            // 按分类ID精确查询
            if(goodstypeId != null){
                wrapper.eq(Goods::getGoodstype, goodstypeId);
            }

            IPage<Goods> result = goodsService.page(page, wrapper);
            return Result.success(result.getRecords(), result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败: " + e.getMessage());
        }
    }
}